//
//  EllipsePageControl+ItemControl.m
//  CoworkClient
//
//  Created by YueWen on 2018/9/27.
//  Copyright © 2018 YueWen. All rights reserved.
//

#import "EllipsePageControl+ItemControl.h"
#import <RITLKit/RITLKit.h>
#import <objc/runtime.h>

static NSString *EllipsePageControl_Scale = @"EllipsePageControl_Scale";

@implementation EllipsePageControl (ItemControl)

+ (void)load {
    
    [self ritl_swizzledInstanceSelector:@selector(createPointView) swizzled:@selector(el_createPointView)];
    [self ritl_swizzledInstanceSelector:@selector(exchangeCurrentView:new:) swizzled:@selector(el_exchangeCurrentView:new:)];
}

- (void)setScale:(CGFloat)scale
{
   if (scale <= 0) { scale = 2; }//默认处理
    objc_setAssociatedObject(self,&EllipsePageControl_Scale, @(scale), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (CGFloat)scale
{
    NSNumber *scale = objc_getAssociatedObject(self, &EllipsePageControl_Scale);
    if (scale == nil) { return 2; }
    return scale.floatValue;
}


- (void)el_createPointView
{
    if (self.scale <= 2) {
        [self el_createPointView]; return;
    }
    
    [self clearView];
    if (self.numberOfPages <= 0) { return; }
    
    //居中控件
    CGFloat startX = 0;
    CGFloat startY = 0;
    CGFloat mainWidth = self.numberOfPages * (self.controlSize + self.controlSpacing);
    if(self.frame.size.width < mainWidth){
        startX = 0;
    }else{
        startX = (self.frame.size.width - mainWidth) / 2;
    }
    if(self.frame.size.height < self.controlSize){
        startY = 0;
    }else{
        startY = (self.frame.size.height - self.controlSize) / 2;
    }
    //动态创建点
    for (int page = 0; page < self.numberOfPages; page++) {
        if(page == self.currentPage){
            UIView *currPointView = [[UIView alloc]initWithFrame:CGRectMake(startX, startY, self.controlSize * self.scale, self.controlSize)];
            currPointView.layer.cornerRadius = self.controlSize/2;
            currPointView.tag = page + 1000;
            currPointView.backgroundColor = self.currentColor;
            currPointView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction:)];
            [currPointView addGestureRecognizer:tapGesture];
            [self addSubview:currPointView];
            startX = CGRectGetMaxX(currPointView.frame) + self.controlSpacing;
            
            if(self.currentBkImg){
                currPointView.backgroundColor = [UIColor clearColor];
                UIImageView *currBkImg = [UIImageView new];
                currBkImg.tag = 1234;
                currBkImg.frame = CGRectMake(0, 0, currPointView.frame.size.width, currPointView.frame.size.height);
                currBkImg.image = self.currentBkImg;
                [currPointView addSubview:currBkImg];
            }
            
        }else{
            
            UIView *otherPointView = [[UIView alloc]initWithFrame:CGRectMake(startX, startY, self.controlSize, self.controlSize)];
            otherPointView.backgroundColor = self.otherColor;
            otherPointView.tag = page + 1000;
            otherPointView.layer.cornerRadius = self.controlSize / 2;
            otherPointView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickAction:)];
            [otherPointView addGestureRecognizer:tapGesture];
            [self addSubview:otherPointView];
            startX = CGRectGetMaxX(otherPointView.frame)+self.controlSpacing;
        }
    }
}


- (void)el_exchangeCurrentView:(NSInteger)old new:(NSInteger)new {
    
    if (self.scale <= 2) {
        [self el_exchangeCurrentView:old new:new]; return;
    }
    
    
    UIView *oldSelect = [self viewWithTag:1000 + old];
    CGRect mpSelect = oldSelect.frame;
    
    UIView *newSeltect = [self viewWithTag:1000+new];

    
    if(self.currentBkImg){
        UIView *imgview = [oldSelect viewWithTag:1234];
        [imgview removeFromSuperview];
        
        newSeltect.backgroundColor = [UIColor clearColor];
        UIImageView *currBkImg = [UIImageView new];
        currBkImg.tag = 1234;
        currBkImg.frame = CGRectMake(0, 0, mpSelect.size.width, mpSelect.size.height);
        currBkImg.image = self.currentBkImg;
        [newSeltect addSubview:currBkImg];
    }
    
    oldSelect.backgroundColor = self.otherColor;
    newSeltect.backgroundColor = self.currentColor;
    
    [UIView animateWithDuration:0.3 animations:^{
        [self changedFrameWithCurrentIndex:new];
    }];
    
}



- (void)changedFrameWithCurrentIndex:(NSInteger)current
{
    //开始变换
    for (NSInteger i = 0; i < self.numberOfPages; i ++) {
        
        UIView *beforeView = [self viewWithTag:i + 1000 - 1];
        UIView *currentView = [self viewWithTag:i + 1000];
        
        CGFloat originX = i == 0 ? 0 : beforeView.ritl_maxX + self.controlSpacing;
        CGFloat originY = currentView.ritl_originY;
        CGFloat height = currentView.ritl_height;
        CGFloat width = current == i ? self.controlSize * self.scale : self.controlSize;

        currentView.frame = CGRectMake(originX, originY, width, height);
    }

}

@end
